Product : ISaGRAF NT target v3.21

Date    : 05 December 1998

File    : SharData.How to share data between 2 NTkernels running on 1 PC.htm

Subject : How to share data between 2 NT kernels running on 1 PC

Keywords: NT target - Shared variables - OpenFileMapping -

          MapViewOfFile - CreateFileMapping

____________________________________________________________________

Here is at the end of this file an extract of code that shows how to

share memory between 2 processes.

The sharing code may be implemented inside an I/O board driver.

(see I/O programmer's guide to know how to fill I/O board

drivers for NT ISaGRAF kernel).

It may too be implemented inside a C Function or a C Function

Block.(see "Target user's guide" of the User's guide,

chapter 'C Programming', and

see too the text file automatically installed with your NT

target, to know how to write a C Function or C Function Block that

will be declared in ISaGRAF Workbench and that can be called

from your IEC application)

To write into shared memory, the writing process may call

CreateFileMapping (and then MapViewOfFile, as in the code below)

Then, if the value to share is 10, it may do for example:

*pMem++ = 10;

*pMem=0;

 

To read into shared memory, the reading process may call

OpenFileMapping (and then MapViewOfFile as in the code below)

Then, to read the pointed shared area, it may do for example:

int ReadValue =*pMem++;

if (*pMem!=0) error();

 

DWORD MapMemory( WORD wSlave, WORD wSpace, HANDLE **hRetMap,

void **pRetMem )

{

char MemName[ 256 ];

HANDLE hMap;

LPVOID pMem;

sprintf( MemName, "ISaGRAFMapMem%d%d" , wSlave, wSpace );

hMap = OpenFileMapping( FILE_MAP_ALL_ACCESS, FALSE, MemName );

// or hMap = CreateFileMapping( (HANDLE) 0xFFFFFFFF, NULL,

// PAGE_READWRITE, 0, size , MemName);

if ( hMap == NULL )

return CAN_T_FIND_RESOURCE ;

/* Get a pointer to the file-mapped shared memory. */

pMem = MapViewOfFile(

hMap, /* object to map view of */

FILE_MAP_ALL_ACCESS, /* read access */

0, /* high offset: map from */

0, /* low offset: beginning */

0); /* default: map entire file */

if ( pMem == NULL)

return ACCESS_DENIED;

*hRetMap = hMap ;

*pRetMem = pMem ;

return( NO_ERROR ) ;

}

You may add a named mutex (Mutual Exclusion object) to ensure

synchonism between the kernel that writes into shared memory

and the kernel that reads from shared memory.

____________________________________________________________________

Copyright © 1996-2009 ICS Triplex ISaGRAF Inc. All rights reserved.